﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Zhuang.DAL;

namespace Zhuang.WebSite.BLL.SystemManagement
{
    public class Sys_MenuBLL
    {

        Sys_MenuDAL sys_MenuDAL = new Sys_MenuDAL();

        string json = "";

        public string GetMenus_System()
        {

            DataTable dt = sys_MenuDAL.GetMenus_System();

            return Zhuang.Utility.Web.JSON.JsonDataHelper.DataTableToJSON(dt);

        }


        /// <summary>
        /// 将Tree菜单的DataTable数据转成JSON
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public string DataTableToJSON(DataTable dt, int parentId)
        {
            json = "[";

            FindTreeNode(dt, parentId);

            json = json + "]";
            return json;
        }


        private void FindTreeNode(DataTable dt, int parentId)
        {
            foreach (DataRow dr in dt.Select("parentId=" + parentId))
            {
                if (json.Substring(json.Length - 1, 1) == "}")
                {
                    json = json + ",";
                }
                //?????
                json = json + "{text:'" + dr["menuName"] + "',url:'" + dr["menuUrl"] + "',isexpand:false";
                //?????
                if (dt.Select("parentId=" + Convert.ToInt32(dr["menuId"])).Length > 0)
                {
                    json = json + ",children:[";

                    FindTreeNode(dt, Convert.ToInt32(dr["menuId"]));

                    json = json + "]";
                }

                json = json + "}";

            }
        }

        public string GetUserMenus(string userId)
        {
            DataTable dt = sys_MenuDAL.GetUserMenus(userId);
            return Zhuang.Utility.Web.JSON.JsonDataHelper.DataTableToJSON(dt);
        }


        public DataTable GetMenuButtons(string roleId, string menuId)
        {
            return sys_MenuDAL.GetMenuButtons(roleId,menuId);
        }


        public string GetRoleMenus(int roleId)
        {

            DataTable dt = sys_MenuDAL.GetRoleMenus(roleId);

            return Zhuang.Utility.Web.JSON.JsonDataHelper.DataTableToJSON(dt);

        }
    }
}
